<?php

namespace App\Repository\Repositories;
/**
 * 用户统计
 * 7.25
 * wsl
 */

use App\Models\StoreCoupon;
use App\Models\StoreCouponIssueUser;
use App\Models\StoreOrder;
use App\Models\StoreProduct;
use App\Models\StoreSeckill;
use App\Models\User;
use App\Models\UserRecharge;
use App\Repository\RepositoryInterface\UserStatisticisInterface;
use Illuminate\Validation\ValidationException;

class UserStatisticisRepositories implements UserStatisticisInterface
{
    /**
     * @return void
     * 用户信息获取
     */
    public function userMessage(){
        try {
            $user_message = User::get(['uid','real_name']);
            if ($user_message){
                return $user_message;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @return false|mixed|string
     * 获取用户手机号
     */
    public function userPhone($user_id){
        try {
            $user_message = User::where('uid' , $user_id)->get(['phone']);
            if ($user_message){
                return $user_message;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $phone
     * @return false|string
     * 获取用户昵称
     */
    public function userPhoneName($phone){
        try {
            $user_message = User::where('phone' , $phone)->get(['real_name']);
            if ($user_message){
                return $user_message;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_name
     * @return false
     * 用户id获取
     */
    public function searchUser($user_name){
        try {
        $user_name_message = User::where('real_name' , $user_name)->first('uid');
        if ($user_name_message){
            return $user_name_message;
        }else{
            return false;
        }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @return false
     * 用户名获取
     */
    public function searchUserName($user_id){
        try {
        $user_name_message = User::where('uid' , $user_id)->first('real_name');
        if ($user_name_message){
            return $user_name_message;
        }else{
            return false;
        }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @return false
     * 用户新增统计
     */
    public function UserAddStatistics($begin,$end){
        try {
        $user = User::where('add_time' , '>' ,$begin)->where('add_time' , '<' ,$end)->get(['uid','real_name','add_time']);
        if ($user){
            return $user;
        }else{
            return false;
        }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }


    /**
     * @return false
     * 用户新增统计
     */
    public function UserAddStatisticsCount(){
        try {
        $user = User::get();
        $user_count = count($user);
        if ($user_count){
            return $user_count;
        }else{
            return false;
        }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }


    /**
     * @param $user_id
     * @return false|string
     * 用户充值统计
     */
    public function userRecharge($user_id){
        try {
            $query_recharge = UserRecharge::where('uid',$user_id)->get('price');
            if ($query_recharge){
                return $query_recharge;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @return false
     * 用户支付金额统计
     */
    public function paymentStatistics($user_id,$begin,$end){
        try {
        $payment_statistics = StoreOrder::where('uid' , $user_id)->where('pay_time' , '>' ,$begin)->where('pay_time' , '<' ,$end)->get('total_price');
        if ($payment_statistics){
            return $payment_statistics;
        }else{
            return false;
        }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @return false|mixed|string
     * 用户详细信息
     */
    public function userDetail($user_id){
        try {
            $user_detail = User::where('uid' , $user_id)->first();
            if ($user_detail){
                return $user_detail;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $goods_id
     * @return false|mixed|string
     * 轮播图
     */
    public function carouselDiagram($goods_id){
        try {
            $goods_carousel_diagram = StoreProduct::where('id' , $goods_id)->get(['id','slider_image']);
            if ($goods_carousel_diagram){
                return $goods_carousel_diagram;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @return false|string
     * 更多优惠券
     */
    public function couponsList(){
        try {
            $store_coupon = StoreCoupon::get();
            if ($store_coupon){
                return $store_coupon;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $id
     * @param $coupon_id
     * @return bool|mixed|string
     * @throws \ErrorException
     * 领取优惠券
     */
    public function couponCollection($id,$coupon_id)
    {
        try {
            if (!is_numeric($id) || !is_numeric($coupon_id)) {
                return '参数不正确';
            }
            $userAttached = StoreCouponIssueUser::where('uid', $id)->where('issue_coupon_id', $coupon_id)->first();
            if ($userAttached) {
                return '已经领取过了';
            }
            $addTime = strtotime(date('Y-m-d H:i:s'));
            $coupon_collection = StoreCouponIssueUser::insert(['uid' => $id, 'issue_coupon_id' => $coupon_id, 'add_time' => $addTime]);
            if ($coupon_collection){
                return true;
            }else{
                return false;
            }
        } catch (ValidationException $validationException) {
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @return false|mixed|string
     * 优惠券列表
     */
    public function meCoupon($user_id){
        try {
            $me_coupon = StoreCouponIssueUser::where('uid' , $user_id)->where('status' , 1)->get();
            if ($me_coupon){
                return $me_coupon;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $goods_id
     * @param $status
     * @return bool|mixed|string
     * 秒杀商品更改状态
     */
    public function spikeStatus($goods_id,$status){
        try {
            $seckill_status = StoreSeckill::where('product_id' , $goods_id)->update(['status' => $status]);
            if ($seckill_status){
                return true;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $goods_id
     * @return false|string
     * 查询秒杀商品状态
     */
    public function spikeStatusQuery($goods_id){
        try {
            $status = StoreSeckill::where('product_id' , $goods_id)->first('status');
            if ($status){
                return $status;
            }else{
                return false;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }
}
